package com.springboot.demo.custom.annotation;

import com.springboot.demo.util.ContextWareUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 自定义注解工具类
 *
 * @author supu
 * @date 2019-03-26 17:18
 **/
public class AnnotationUtil {
    public static void getInfo(Class<?> clazz) {
        // 自定义注解属性解析
        Field fields[] = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Name.class)) {
                Name arg0 = field.getAnnotation(Name.class);
                System.out.println("address=" + arg0.value());
            }
        }

        // 自定义注解方法解析
        Method[] methods = clazz.getDeclaredMethods();
        for(Method method : methods){
            if(method.isAnnotationPresent(com.springboot.demo.custom.annotation.Method.class)){
                com.springboot.demo.custom.annotation.Method customMethod = method.getAnnotation(com.springboot.demo.custom.annotation.Method.class);
                if(customMethod != null){
                    Class<?>[] classes = customMethod.value();
                    if(classes != null){
                        for(Class clz : classes){
                            BeforePlugin beforePlugin = (BeforePlugin) ContextWareUtil.getBean(clz);
                            beforePlugin.interceptor(method);
                        }
                    }
                }
            }
        }
    }
}
